package tagdb.algorithm.tag;

/**
 * <b>Tag</b>
 * <p>
 * This class holds a sequence tag, i.e. a short (3-6 amino acids long) version of a peptide sequence.
 * </p>
 *
 * @author Thilo Muth
 */
public class Tag implements Comparable<Tag> {

    /**
     * This variable holds the formatted sequence without mass gaps.
     */
    private String sequence;

    /**
     * The total mass of the tag.
     */
    private double totalMass;

    /**
     * The number of times the tag was matched to a database sequence during filtering.
     */
    private int matchCount = 0;

    /**
     * Tag type.
     */
    private TagType tagType;

    /**
     * Tag charge.
     */
    private int charge;

    /**
     * Tag score.
     */
    private double score;

    /**
     * TagType enum: N-terminal / C-terminal.
     */
    public enum TagType {
        N_TERMINAL, C_TERMINAL
    }

    /**
     * Constructs a tag on the basis of the sequence
     *
     * @param sequence Tag sequence
     */
    public Tag(String sequence) {
        this.sequence = sequence;
    }

    /**
     * Constructs a tag with sequence, tag type and charge.
     */
    public Tag(String sequence, TagType tagType, int charge) {
        this.sequence = sequence;
        this.tagType = tagType;
        this.charge = charge;
    }

    /**
     * Returns the formatted sequence with gaps.
     *
     * @return The formatted sequence.
     */
    public String getSequence() {
        return sequence;
    }

    /**
     * The total mass of the tag.
     *
     * @return The total mass of the tag.
     */
    public double getTotalMass() {
        return totalMass;
    }

    /**
     * Returns the length
     * 
     * @return the length
     */
    public int getLength() {
        return sequence.length();
    }

    /**
     * Returns the match count.
     * @return  The number of times the tag was matched during database filtering.
     */
    public int getMatchCount() {
        return matchCount;
    }

    /**
     * Increases the match count by 1.
     */
    public void increaseMatchCount() {
        this.matchCount++;
    }

    /**
     * Returns the tag type.
     * @return Tag type.
     */
    public TagType getType() {
        return tagType;
    }

    /**
     * Returns the tag charge.
     * @return Tag charge.
     */
    public int getCharge() {
        return charge;
    }

    /**
     * Sets the tag score.
     * @param score Tag score.
     */
    public void setScore(double score) {
        this.score = score;
    }

    /**
     * Returns the tag score.
     * @return Tag score.
     */
    public double getScore() {
        return score;
    }

    /**
     * Compares the object to another tag by its score, necessary for sorting of tags by their scores.
     * @param tag Another tag.
     * @return Integer value
     */
    public int compareTo(Tag tag) {
        if(this.score > tag.getScore()) {
            return -1;
        } else if (this.score < tag.getScore()){
            return 1;
        } else {
            // Only same-score and same-sequence tags are considered as equal.
            if(this.sequence.equalsIgnoreCase(tag.getSequence())){
                return 0;
            } else {
                return -1;
            }

        }
    }
}
